﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using DingY.DB;

namespace DotNet.WebUI
{
    /// <summary>
    /// 基本页面
    /// </summary>
    public class BasicPage : System.Web.UI.Page
    {
        public BasicPage()
        {

        }

        #region 访问Access数据库

        string _ConnString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + @"|DataDirectory|\ASPNET.mdb;Persist Security Info=True;";

        private OleDbConnection GetOleDbConn()
        {
            OleDbConnection objConn = new OleDbConnection();
            objConn.ConnectionString = _ConnString;
            objConn.Open();

            return objConn;
        }

        public DataTable ExecuteDataTable(string Sql)
        {
            OleDbCommand objCmd = new OleDbCommand();
            objCmd.Connection = GetOleDbConn();
            objCmd.CommandText = Sql;

            OleDbDataAdapter objDa = new OleDbDataAdapter();
            objDa.SelectCommand = objCmd;

            DataSet objDS = new DataSet();
            objDa.Fill(objDS);

            return objDS.Tables[0];
        }

        public DataTable ExecuteDataTable(string Sql, int PageIndex, int PageSize)
        {
            OleDbCommand objCmd = new OleDbCommand();
            objCmd.Connection = GetOleDbConn();
            objCmd.CommandText = Sql;

            OleDbDataAdapter objDa = new OleDbDataAdapter();
            objDa.SelectCommand = objCmd;

            DataSet objDS = new DataSet();
            objDa.Fill(objDS, (PageIndex - 1) * PageSize, PageSize, "DingYTable");

            return objDS.Tables[0];
        }

        public int ExecuteNonQuery(string Sql)
        {
            OleDbCommand objCmd = new OleDbCommand();
            objCmd.Connection = GetOleDbConn();
            objCmd.CommandText = Sql;
            return objCmd.ExecuteNonQuery();
        }

        public object ExecuteScalar(string Sql)
        {
            OleDbConnection objConn = new OleDbConnection();
            objConn.ConnectionString = _ConnString;
            objConn.Open();

            OleDbCommand objCmd = new OleDbCommand();
            objCmd.Connection = objConn;
            objCmd.CommandText = Sql;
            return objCmd.ExecuteScalar();
        }

        #endregion

        #region 访问SqlHelper类

        private string GetConnString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["SqlHelper"].ConnectionString;
        }

        public int ExecuteNonQueryBySql(string Sql)
        {
            return SqlHelper.ExecuteNonQuery(GetConnString(), CommandType.Text, Sql);
        }

        public object ExecuteScalarBySql(string Sql)
        {
            return SqlHelper.ExecuteScalar(GetConnString(), CommandType.Text, Sql);
        }

        public DataSet ExecuteDatasetBySql(string Sql)
        {
            return SqlHelper.ExecuteDataset(GetConnString(), CommandType.Text, Sql);
        }

        #endregion
    }
}